<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>Untitled Document</title>
	</head>
	<body>
		<input type="text" size='75' id="text"><br />
		<input type="button" value="parse" id="btn">
		<p>
			<input type="text" size="75" id="out" value="output" readonly>
		</p>
<script type="text/javascript">
	var btn = document.getElementById('btn');
	var text = document.getElementById('text');
	
	btn.addEventListener('click', function(e){
		var text = document.getElementById('text').value;
		document.getElementById('out').value = decodeURIComponent(parse(text));
	}, false);


	// http://search.goo.ne.jp/web.jsp?MT=%E7%94%B0%E7%94%BA&bt_search.x=0&bt_search.y=0&bt_search=%E6%A4%9C%E7%B4%A2&STYPE=web&IE=UTF-8&from=gootop
	var parse = function(str){
		var patterns = [{pat : /^http:\/\/www\.google\.co\.jp\/search\?(.+)$/, q : /^q=(.*)$/},
			{pat : /^http:\/\/search\.goo\.ne\.jp\/web\.jsp\?(.+)$/, q : /^MT=(.*)$/},
			{pat : /^http:\/\/search\.yahoo\.co\.jp\/search\?(.+)$/, q : /^p=(.*)$/} ];
		for (var l = 0; l < patterns.length; l++) {
			var q = 'null';
			if (str.match(patterns[l].pat)) {
				ps = str.match(patterns[l].pat)[1];
				if (ps != null && typeof(ps) == 'string') {
					a = ps.split('&');
					for (var i = 0; i < a.length; i++) {
						if (a[i].match(patterns[l].q)) {
							q = a[i].match(patterns[l].q)[1]
							l = patterns.length;
							break;
						}
					}
				}
			}
		}
		return q;
	}
</script>
	</body>
</html>
